package com.shop.zkmall.app.service.a.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.zkmall.app.domain.CereBuyerGift;
import com.shop.zkmall.app.domain.CereBuyerGiftRecord;
import com.shop.zkmall.app.domain.req.SendGiftReq;
import com.shop.zkmall.app.mapper.CereBuyerGiftMapper;
import com.shop.zkmall.app.mapper.CereBuyerGiftRecordMapper;
import com.shop.zkmall.app.service.a.ICereBuyerGiftRecordService;
import com.shop.zkmall.app.service.buyer.CereBuyerUserService;
import com.shop.zkmall.commons.domain.buyer.CereBuyerUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 * 直播间礼物赠送记录表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-11-25
 */
@Service
public class CereBuyerGiftRecordServiceImpl extends ServiceImpl<CereBuyerGiftRecordMapper, CereBuyerGiftRecord> implements ICereBuyerGiftRecordService {

    @Autowired
    private CereBuyerGiftMapper cereBuyerGiftMapper;
    @Autowired
    private CereBuyerUserService cereBuyerUserService;
    @Autowired
    private CereBuyerBalanceDetailServiceImpl cereBuyerBalanceDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void sendGift(Long userId, Long anchorId, SendGiftReq sendGiftReq) {
        //查询礼物信息
        CereBuyerGift byId = cereBuyerGiftMapper.selectById(sendGiftReq.getGiftId());
        if (byId == null) {
            return;
        }
        //用户
        CereBuyerUser cereBuyerUser = cereBuyerUserService.selectByBuyerUserId(userId);
        //主播
        CereBuyerUser anBuyerUser = cereBuyerUserService.selectByBuyerUserId(anchorId);
        //礼物单价
        BigDecimal price = byId.getPrice();
        //判断余额是否充足
        if (cereBuyerUser.getBalance().compareTo(price.multiply(BigDecimal.valueOf(sendGiftReq.getGiftNum()))) < 0) {
            throw new IllegalArgumentException("余额不足");
        }
        BigDecimal sum = BigDecimal.valueOf(sendGiftReq.getGiftNum());
        BigDecimal totalPrice = price.multiply(sum);

        CereBuyerGiftRecord cereBuyerGiftRecord = new CereBuyerGiftRecord();
        cereBuyerGiftRecord.setGiftId(sendGiftReq.getGiftId());
        cereBuyerGiftRecord.setGiftName(byId.getGiftName());
        cereBuyerGiftRecord.setGiftIcon(byId.getGiftIcon());
        cereBuyerGiftRecord.setPrice(price);
        cereBuyerGiftRecord.setQuantity(sendGiftReq.getGiftNum());
        cereBuyerGiftRecord.setTotalPrice(totalPrice);
        cereBuyerGiftRecord.setSenderId(userId);
        cereBuyerGiftRecord.setReceiverId(anchorId);
        save(cereBuyerGiftRecord);
        //添加 增加余额的明细
        cereBuyerBalanceDetailService.addBalanceDetail(anBuyerUser, totalPrice,
                cereBuyerUser.getName() + "打赏" + byId.getGiftName());
        cereBuyerUserService.addBalance(anchorId, totalPrice);

        //减少用户余额 以及明细
        cereBuyerBalanceDetailService.subtractBalanceDetail(
                cereBuyerUser, totalPrice, anBuyerUser.getName() + "打赏" + byId.getGiftName());
        cereBuyerUserService.subtractBalance(cereBuyerUser.getBuyerUserId(), totalPrice);
    }
}
